package com.example.nonameaa.controller;

import cn.hutool.core.util.StrUtil;
import com.example.nonameaa.Common.LogType;
import com.example.nonameaa.utils.AopLogs;
import com.example.nonameaa.utils.AuthAccess;
import com.example.nonameaa.Common.Result;
import com.example.nonameaa.entity.User;
import com.example.nonameaa.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能：
 * 作者：lhp
 * 日期：2024/9/25 12:21
 */

@RestController
public class LoginController {

    @Resource
    UserService userService;


    @PostMapping("/login")
    @AopLogs(operation = "用户", type = LogType.LOGIN)
    public Result login(@RequestBody User user) {
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error("帐号或密码不能为空");
        }
     user  = userService.loginUser(user);
    return Result.success(user);
    }
    @AopLogs(operation = "用户", type = LogType.REGISTER)
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword()) || StrUtil.isBlank(user.getRole())) {
            return Result.error("数据输入不合法");
        }
        if (user.getUsername().length() > 10 || user.getPassword().length() > 20) {
            return Result.error("数据输入不合法");
        }
        user = userService.register(user);
        return Result.success(user);

    }
    /**
     *  Controller重置密码
     */

    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPhone())) {
            return Result.error("数据输入不合法");
        }
        userService.resetPassword(user);
        return Result.success();
    }
}